package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.MaterialBiz;
import com.yc.sole.erp.request.MaterialQueryReq;
import com.yc.sole.erp.request.MaterialReq;
import com.yc.sole.erp.response.MaterialResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yizuomin
 * @date 2024-01-24
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class MaterialController {

    private final MaterialBiz materialBiz;

    /**
     * 新增
     *
     * @param materialReq
     * @return RestResult
     */
    @PreAuth(value = "erp:material:save", description = "新增材料")
    @PostMapping("/api/erp/material/saveMaterial")
    public Result<Void> saveMaterial(@RequestBody @Validated(SaveGroup.class) MaterialReq materialReq) {
        materialBiz.saveMaterial(materialReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param materialReq
     * @return RestResult
     */
    @PreAuth(value = "erp:material:update", description = "修改材料")
    @PostMapping("/api/erp/material/updateMaterialById")
    public Result<Void> updateMaterialById(@RequestBody @Validated(UpdateGroup.class) MaterialReq materialReq) {
        materialBiz.updateMaterialById(materialReq);
        return Result.success();
    }

    /**
     * 通过id批量删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:material:delete", description = "删除材料")
    @PostMapping("/api/erp/material/deleteMaterial")
    public Result<Void> deleteMaterial(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        materialBiz.deleteMaterialByIds(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 查询
     *
     * @param materialQueryReq
     * @return Result
     */
    @PreAuth(value = "erp:material:select", description = "查询材料")
    @PostMapping("/api/erp/material/selectMaterialByName")
    public Result<List<MaterialResp>> selectMaterialByName(@RequestBody MaterialQueryReq materialQueryReq) {
        return Result.data(materialBiz.selectMaterialByName(materialQueryReq.getName()));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return Result
     */
    @PreAuth(value = "erp:material:select", description = "查询材料")
    @PostMapping("/api/erp/material/selectMaterialById/{id}")
    public Result<MaterialResp> selectMaterialById(@PathVariable Long id) {
        return Result.data(materialBiz.selectMaterialById(id));
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:material:select", description = "查询材料")
    @PostMapping("/api/erp/material/selectMaterialPage")
    public Result<Paging<MaterialResp>> selectMaterialPage(@RequestBody @Validated BasePageReq<MaterialQueryReq> basePageReq) {
        return Result.data(materialBiz.selectMaterialPage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
